<?php

defined('MYLITS') ? '' : exit('No direct script access allowed');

/**
 * @version     1.0.0
 * @package     mylits engine
 * @license     GNU General Public License version 3
 * @author      Arfan Fudyartanto D N <arfan@mylits.com> - http://engine.mylits.com
 */
class Mylits_Loader {

    private static $instance;

    private function __construct() {
        
    }

    /**
     * This method will return Mylits_Loader object
     * 
     * @return object Mylits_Loader
     */
    public static function getInstance() {
        if (is_null(self::$instance)) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    public static function getController($name, $path = NULL) {
        $temp = explode('.', $name);
        $cname = end($temp);
        if (!empty($path)) {
            
        } else {
            $path = APP_DIR . '/components/comp.' . $name . '/' . $cname . '.ctrl.php';
        }
        $componentClass = $cname . 'Controller';
        if (is_file($path)) {
            require_once $path;
            $obj = new $componentClass;
            return $obj;
        } else {
            echo '<pre>Controller ' . $cname . ' not found</pre>';
            exit;
        }
    }

    public static function getModel($name, $path = NULL) {
        if (empty($path)) {
            $path = '';
        }
        if (is_file($path)) {
            require_once $path;
            $class = $name . 'Model';
            $obj = new $class;
            return $obj;
        } else {
            echo 'Class ' . $class . ' not found';
        }
    }

    public static function getObject($path) {
        if (is_file($path)) {
            
        } else {
            echo "File not found";
        }
    }

    /**
     * Get library
     * 
     * @param string $name
     * @return Object Library
     */
    public static function getLibrary($name) {
        $path = APP_DIR . 'libs/' . $name . '/' . $name . '.php';
        if (is_file($path)) {
            require_once $path;
            $lib = new $name;
            return $lib;
        } else {
            echo 'Library ' . $name . ' not found';
        }
    }

}

?>
